package com.jg.xmlSchema2JsonSchema;

import java.util.ArrayList;
import java.util.List;

import com.jg.xmlSchema2JsonSchema.xsd.XsAttribute;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author czg
 */
public class XmlUtil {

    /**
     * 获得根节点
     *
     * @param document
     * @param rootElementName
     * @return
     */
    public static Element getRootElement(Document document, String rootElementName) {
        NodeList configNodeList = document.getElementsByTagName(rootElementName);
        if (configNodeList.getLength() > 0) {
            return (Element) configNodeList.item(0);
        }
        return null;
    }

    /**
     * 从属性数组中获得给定名称的属性
     *
     * @param attributes
     * @param attributeName
     * @return
     */
    public static XsAttribute getAttributeOfName(XsAttribute[] attributes, String attributeName) {

        for (XsAttribute mXsAttribute : attributes) {
            if (mXsAttribute.getName().equals(attributeName)) {
                return mXsAttribute;
            }
        }
        return null;
    }

    /**
     * 获得全部子项
     *
     * @param parentElement 父节点
     * @param childNodeName 子节点名称
     * @return
     */
    public static List<Element> getChildElements(Element parentElement, String childNodeName) {

        List<Element> elementList = new ArrayList<Element>();
        NodeList dateTypeList = parentElement.getChildNodes();
        for (int n = 0; n < dateTypeList.getLength(); n++) {
            Node node = dateTypeList.item(n);
            if (node.getNodeName().equals(childNodeName)) {
                elementList.add((Element) node);
            }
        }
        return elementList;
    }

    /**
     * 获得给定的子项
     *
     * @param parentElement
     * @param childNodeNames
     * @return
     */
    public static List<Element> getChildElements(Element parentElement, String[] childNodeNames) {
        List<Element> elementList = new ArrayList<Element>();
        NodeList dateTypeList = parentElement.getChildNodes();
        for (int n = 0; n < dateTypeList.getLength(); n++) {
            Node node = dateTypeList.item(n);
            for (int i = 0; n < childNodeNames.length; i++) {
                if (node.getNodeName().equals(childNodeNames[i])) {
                    elementList.add((Element) node);
                    break;
                }
            }
        }
        return elementList;
    }

    /**
     * 获得子项
     *
     * @param parentElement 父节点
     * @param childNodeName 子节点名称
     * @return
     */
    public static Element getChildElement(Element parentElement, String childNodeName) {
        NodeList childNodeList = parentElement.getChildNodes();
        for (int n = 0; n < childNodeList.getLength(); n++) {
            Node node = childNodeList.item(n);
            if (node.getNodeName().equals(childNodeName)) {
                return (Element) node;
            }
        }
        return null;
    }
}
